using UnityEngine;

public class CharMovement : DrawableGameObject 
{
    public Texture2D[] standTextures, runTextures, jumpTextures, crouchTextures, attackTextures;
	private float horizontal;
	private float vertical;
	public float speed;
	public float jumpSpeed;

	private bool grounded;
    private bool attacking;
    private bool crouching;

    public float endPosition;
    public string nextFase;
	
    private BoxCollider boxCollider;
	
	private int coinCounter;
	public TextMesh coinText;
	
	// Use this for initialization
	public override void Start () 
	{
        base.SetAnimations(standTextures, runTextures, jumpTextures, crouchTextures, attackTextures);
        base.Start();

        boxCollider = GetComponent<BoxCollider>();
	}
	
	// Update is called once per frame
    public void Update()
	{
        Move();
        RunAnimations();

        if (transform.position.x > endPosition)
        {
            Application.LoadLevel(nextFase);
        }
		
		coinText.text = "Coins: " + coinCounter;
		
		if(coinCounter == 50)
		{
			if(GetComponent<HP>().getLife() < 5)
			{
				GetComponent<HP>().increaseLife(1);
				coinCounter = 0;
			}
		}
    }

    void Move()
    {
        if (grounded)
        {
            horizontal = Input.GetAxis("Horizontal") * speed;
        }

        if (Input.GetKeyDown(KeyCode.Space) && grounded)
        {
            crouching = false;
            vertical = jumpSpeed;
            rigidbody.velocity += new Vector3(0, vertical, 0);
        }

        if (Input.GetKey(KeyCode.DownArrow) && grounded || Input.GetKey(KeyCode.S) && grounded)
        {
            attacking = false;
            crouching = true;
        }

        if (Input.GetKeyUp(KeyCode.DownArrow) || Input.GetKeyUp(KeyCode.S))
        {
            crouching = false;
        }

        transform.Translate(transform.InverseTransformDirection(horizontal * Time.deltaTime, 0, 0));
    }

    void RunAnimations()
	{
		if(horizontal == 0 && grounded && attacking == false)
		{
            base.gameAnimation.fps = 15;
            base.gameAnimation.CalculateFps();
            base.RunAnimation("Stand", "Loop");
		}
		
		if(horizontal > 0 && grounded)
		{
            gameObject.transform.localScale = scale;

            attacking = false;
            crouching = false;
            base.gameAnimation.fps = 15;
            base.gameAnimation.CalculateFps();
            base.RunAnimation("Run", "Loop");
		}

        if (horizontal < 0 && grounded)
		{
            gameObject.transform.localScale = new Vector3(-scale.x, scale.y, scale.z);

            attacking = false;
            crouching = false;
            base.gameAnimation.fps = 15;
            base.gameAnimation.CalculateFps();
            base.RunAnimation("Run", "Loop");
		}

        if (vertical != 0)
        {
            attacking = false;
            base.gameAnimation.fps = 1;
            base.gameAnimation.CalculateFps();
            base.RunAnimation("Jump", "Loop");
        }

        if (Input.GetKeyDown("z") || Input.GetKeyDown("k"))
        {    
            attacking = true;
            crouching = false;
            base.gameAnimation.fps = 35;
            base.gameAnimation.CalculateFps();
            base.RunAnimation("Attack", "OneShot");
        }

        if (crouching)
        {
            horizontal = 0;
            boxCollider.center = new Vector3(-0.14f, 0, 1.9f);
            boxCollider.size = new Vector3(6, 2, 6.4f);
            base.RunAnimation("Crouch", "Loop");
        }
        else
        {
            boxCollider.center = new Vector3(-0.14f, 0, 0.5f);
            boxCollider.size = new Vector3(6, 2, 9f); 
        }

        if (base.gameAnimation.isPlaying() == false)
        {
            attacking = false;
        }
	}

    public float GetSpeed() { return horizontal; }

    public bool IsAttacking() { return attacking; }
	
	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "Coin")
		{
			coinCounter++;
		}
	}
	
	void OnCollisionStay(Collision other)
	{
		if(other.gameObject.tag == "Floor")
		{
			grounded = true;
		}

        if (other.gameObject.tag == "Enemy" && attacking)
        {
            Destroy(other.gameObject);
        }
	}
	
	void OnCollisionExit(Collision other)
	{
		if(other.gameObject.tag == "Floor")
		{
			grounded = false;
			vertical = 0;
		}
	}
}
